<template>
  <div class="exchangePAge">
    <div style="color: #101010; font-weight: 600">组合购列表</div>
    <div style="margin-bottom: 15px; display: flex; justify-content: flex-end">
      <el-button type="primary" @click="addExchangeGoods()">创建商品</el-button>
    </div>
    <div>
      <el-table :data="addGoodsList" border style="width: 100%">
        <el-table-column
          label="ID"
          type="index"
          :index="indexMethod"
          align="center"
        />
        <el-table-column
          prop="orderList"
          label="列表排序"
          width="100"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="orderRotation"
          label="主页排序"
          width="100"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          label="组合购名称"
          width="240"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="editTime"
          label="更新日期"
          width="210"
          align="center"
        >
        </el-table-column>
        <!-- <el-table-column prop="listShow" label="列表奖品状态" width="130">
          <template slot-scope="scope">
            <span v-if="!scope.row.listShow">下架</span>
            <span v-else>上架</span>
          </template>
        </el-table-column>
        <el-table-column prop="rotationShow" label="主页奖品状态" width="130">
          <template slot-scope="scope">
            <span v-if="!scope.row.rotationShow">下架</span>
            <span v-else>上架</span>
          </template>
        </el-table-column> -->
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              v-no-more-click
              v-if="scope.row.listShow"
              @click="controlGoods(scope.row)"
              type="warning"
              size="mini"
              >列表关闭</el-button
            >
             
            <el-button
              v-no-more-click
              v-else
              @click="doneGoods(scope.row)"
              type="success"
              size="mini"
              >列表展示</el-button
            >
            <el-button
              v-no-more-click
              v-if="scope.row.rotationShow"
              @click="homeDisplay(scope.row)"
              type="warning"
              size="mini"
              >主页关闭</el-button
            >
            <el-button
              v-no-more-click
              v-else
              @click="homeShelf(scope.row)"
              type="success"
              size="mini"
              >主页展示</el-button
            >
            <el-button
              type="primary"
              size="mini"
              @click="editAddGoods(scope.row)"
              >编辑</el-button
            >
            <el-button @click="deleteGoods(scope.row)" type="danger" size="mini"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 30px">
        <el-pagination
          mini
          layout="total, prev, pager, next, jumper"
          :current-page.sync="pageNum"
          :page-size="pageSize"
          :total="totalPage"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
    </div>
    <addGoodsAdd
      v-if="addOrEdit == 1"
      :addOrEdit="addOrEdit"
      @getAddGoodsList="getAddGoodsList"
    ></addGoodsAdd>
    <addGoodsEdit
      v-if="addOrEdit == 2"
      :editGoodsCont="editGoodsCont"
      :addOrEdit="addOrEdit"
      @getAddGoodsList="getAddGoodsList"
    ></addGoodsEdit>
  </div>
</template>
<script>
import addGoodsAdd from "./addGoodsAdd.vue";
import addGoodsEdit from "./addGoodsEdit.vue";
import requestAxios from "../../../http";
export default {
  name: "addGoodsPage",
  components: {
    addGoodsAdd,
    addGoodsEdit,
  },
  data() {
    return {
      gooodsLisr: [], // 商品分类
      addOrEdit: false, //添加新的商品还是编辑
      editGoodsCont: {}, //编辑的商品信息
      addGoodsList: [],
      pageNum: 1,
      pageSize: 10,
      totalPage: 0,
    };
  },
  mounted() {
    this.getAddGoodsList();
  },
  methods: {
    indexMethod(index) {
      return (this.pageNum - 1) * this.pageSize + index + 1;
    },

    //添加
    addExchangeGoods() {
      this.addOrEdit = 1;
    },
    //编辑
    editAddGoods(item) {
      requestAxios
        .fetchGet(
          "/api/v2.1/crossStorePurchaseConfig/crossStorePurchaseItemInfo/?id=" +
            item.id,
          {},
          {
            headers: {},
          }
        )
        .then((data) => {
          if (data.status == 200 && data.data.success) {
            console.log("编辑获取组合够列表", data.data);
            this.addOrEdit = 2;
            this.editGoodsCont = data.data.data;
            console.log(this.editGoodsCont, "1111111111134445");
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //获取组合够列表信息
    getAddGoodsList() {
      let obj = null;
      obj = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      };
      requestAxios
        .fetchPost(
          "/api/v2.1/crossStorePurchaseConfig/crossStorePurchaseItemInfo",
          obj,
          {
            headers: {},
          }
        )
        .then((data) => {
          console.log(data, "____)))))))))))))))))))))))))0");
          if (data.status == 200 && data.data.success) {
            console.log("获取组合够列表", data.data);
            this.addGoodsList = data.data.data.aaData;
            this.totalPage = data.data.data.iTotalDisplayRecords;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //改变当前页数
    handleCurrentChange(val) {
      let that = this;
      that.pageNum = val;
      console.log(`当前页: ${that.pageNum}`);
      that.getAddGoodsList();
    },
    //删除
    deleteGoods(item) {
      console.log("删除某个奖品", item);
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteConfirm(item.id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    deleteConfirm(ids) {
      requestAxios
        .fetchGet(
          "/api/v2.1/crossStorePurchaseConfig/delete/?ids=" + ids,
          {},
          {
            headers: {},
          }
        )
        .then((data) => {
          if (data.status == 200 && data.data.success) {
            console.log("删除奖品", data.data);
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            let totalPage = Math.ceil((this.totalPage - 1) / this.pageSize);
            let currengpage = this.pageNum > totalPage ? totalPage : this.pageNum;
            this.pageNum = currengpage < 1 ? 1 : currengpage;
            console.log(this.pageNum);
            this.getAddGoodsList();
          } else {
            console.log("删除奖品", data.data);
            this.tip(false, data.data.errorMsg);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //列表上架
    doneGoods(item) {
      console.log("新增信息确认2", item);
      let obj = null;
      obj = {
        status: 3,
        id: item.id,
      };
      requestAxios
        .fetchPost("/api/v2.1/crossStorePurchaseConfig/modifyShowStatus", obj, {
          headers: {},
        })
        .then((data) => {
          if (data.status == 200 && data.data.success) {
            console.log("新增兑换商品结果", data.data);
            //新增成功
            this.getAddGoodsList();
            this.tip(true, "列表展示成功!");
          } else {
            console.log("删除", data.data);
            this.tip(false, data.data.errorMsg);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //列表下架
    controlGoods(item) {
      console.log("新增信息确认2", item);
      let obj = null;
      obj = {
        status: 4,
        id: item.id,
      };
      requestAxios
        .fetchPost("/api/v2.1/crossStorePurchaseConfig/modifyShowStatus", obj, {
          headers: {},
        })
        .then((data) => {
          if (data.status == 200 && data.data.success) {
            console.log("新增兑换商品结果", data.data);
            //新增成功
            this.getAddGoodsList();
            this.tip(true, "列表关闭成功!");
          } else {
            console.log("删除", data.data);
            this.tip(false, data.data.errorMsg);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //主页上架
    homeShelf(item) {
      console.log("新增信息确认2", item);
      let obj = null;
      obj = {
        status: 1,
        id: item.id,
      };
      requestAxios
        .fetchPost("/api/v2.1/crossStorePurchaseConfig/modifyShowStatus", obj, {
          headers: {},
        })
        .then((data) => {
          if (data.status == 200 && data.data.success) {
            console.log("新增兑换商品结果", data.data);
            //新增成功
            this.getAddGoodsList();
            this.tip(true, "主页展示成功!");
          } else {
            console.log("删除", data.data);
            this.tip(false, data.data.errorMsg);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //主页下架
    homeDisplay(item) {
      console.log("新增信息确认2", item);
      let obj = null;
      obj = {
        status: 2,
        id: item.id,
      };
      requestAxios
        .fetchPost("/api/v2.1/crossStorePurchaseConfig/modifyShowStatus", obj, {
          headers: {},
        })
        .then((data) => {
          if (data.status == 200 && data.data.success) {
            console.log("新增兑换商品结果", data.data);
            //新增成功
            this.getAddGoodsList();
            this.tip(true, "主页关闭成功!");
          } else {
            console.log("删除", data.data);
            this.tip(false, data.data.errorMsg);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //提示的通用方法
    tip(state, text) {
      if (state) {
        this.$message({
          showClose: true,
          message: text,
          type: "success",
        });
      } else {
        this.$message({
          showClose: true,
          message: text,
          type: "error",
        });
      }
    },
  },
};
</script>
<style scoped>
</style>
